jsx3.require("jsx3.gui.Matrix");

/**
* A Pipe protoype for ActiveMatrix Admin UI.  Acts as a container for PipeButtons.
**/
jsx3.Class.defineClass(
        "ux.core.gui.v10.Pipe",
        jsx3.gui.Block,
        null,
        function (pipe) {


            /**
             * instance initializer
             * @param strName {String} unique name distinguishing this object from all other JSX GUI objects in the JSX application
             * @param vntLeft {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
             * @param vntTop {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
             * @param vntWidth {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
             * @param vntHeight {int|String} either a number (i.e, 12, 30, etc) or a number and a unit value (i.e., "25%", "36pt", etc); if a number is passed, pixels will be the assumed unit when painted to screen
             * @param strHTML {String} Text/HTML markup to place in the jsx3.gui.Block instance
             */
            pipe.prototype.init = function(strName, vntLeft, vntTop, vntWidth, vntHeight, strHTML) {
                //call constructor for super class (the super expects the name of the object and the function that it is an instance of)
                this.jsxsuper(strName, vntLeft, vntTop, vntWidth, vntHeight, strHTML);
            };

            /**
            * Callback function for subscriptions to the selection event on PipeButtons.  Changes the look and feel of
            * of the buttons to toggle the state of any previously selected PipeButtons, and show the selected state
            * of the newly selected PipeButton.
            *
            * @param objEvent {Object} the event object published upon selection (with a <code>target</code> field
            *                          containing the actual PipeButton that was selected)
            * @private
            */
            pipe.prototype.setSelected = function(objEvent) {
              // get all buttons
                try {
            var array = this.getDescendantsOfType(ux.core.gui.v10.PipeButton)
               for(var i = 0 ; i< array.length ; i++){
                   if(array[i] != objEvent.target) {
                         array[i].doUnpressed();
//                       array[i].setEnabled(jsx3.gui.Form.STATEENABLED, true)
                   }
                   else {
                       array[i].doPressed();
                   }
               }
//               clickedBTN.setEnabled(jsx3.gui.Form.STATEDISABLED,true )
                } catch(ex) {
                    ux.core.System.logException(ex);
                }
            }

            pipe.prototype.onSetParent = function(parent) {
                this.resetButtonStates();
                //               clickedBTN.setEnabled(jsx3.gui.Form.STATEDISABLED,true )
                return this.jsxsuper(parent);
            }

            pipe.prototype.adoptChild = function(objChild, bRepaint) {
                var ret =  this.jsxsuper(objChild, bRepaint);
                if(objChild instanceof ux.core.gui.v10.PipeButton) {
                    var me = this;
                    objChild.subscribe(jsx3.gui.Interactive.EXECUTE, me, me.setSelected);
                }
                this.resetButtonStates();
                return ret;
            }

            pipe.prototype.resetButtonStates = function() {
                var array = this.getDescendantsOfType(ux.core.gui.v10.PipeButton)
                for(var i = 0; i < array.length; i++) {
                    if(i == 0) {
                        array[i].doPressed();
                    }
                    else {
                        array[i].doUnpressed();
                        //                       array[i].setEnabled(jsx3.gui.Form.STATEENABLED, true)
                    }
                }
            }

            pipe.prototype.setChild = function(objChild, intPersist, strSource, strNS) {
                var ret = this.jsxsuper(objChild, intPersist, strSource, strNS);
                if(objChild instanceof ux.core.gui.v10.PipeButton) {
                    var me = this;
                    objChild.subscribe(jsx3.gui.Interactive.EXECUTE, me, me.setSelected);
                }
                this.resetButtonStates();
                return ret;
            }

            pipe.prototype.onRemoveChild = function(objChild, intIndex) {
                var childArr = objChild;
                if(childArr instanceof jsx3.app.Model) {
                    childArr = [objChild];
                }
                for(var i = 0; i < childArr.length; i++) {
                var child = childArr[i];
                    if(child instanceof ux.core.gui.v10.PipeButton) {
                    var me = this;
                    child.unsubscribe(jsx3.gui.Interactive.EXECUTE, me, me.setSelected);
                }
                }
                var ret = this.jsxsuper(objChild, intIndex);
                this.resetButtonStates();
                return ret;
            };
        }
        )